﻿using Core.Container;
using Core.Interface;
using Core.Log;
using Core.MessageEvent;
using ModBus.Mode;
using Prism.Commands;
using Prism.Dialogs;
using Prism.Events;
using Prism.Ioc;
using Prism.Mvvm;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;

namespace ModBus.ViewModels
{
    public class RTUDevicesViewModel : BindableBase
    {
        Container<ModBusDevice> deviceContainer;
        DataContainer<RTUParam> RTUParams;
        IEventAggregator _ea;
        IDialogService _dialogService;
        Notify notifyCollect;
        public RTUDevicesViewModel(IContainerExtension container, IEventAggregator ea, IDialogService dialogService)
        {
            _dialogService = dialogService;
            _ea = ea;
            deviceContainer = container.Resolve<Container<ModBusDevice>>("ModBusDevice");
            RTUParams = container.Resolve<DataContainer<RTUParam>>("RTUParams");
            var allKeys = deviceContainer?.GetKeys();
            var selectKeys = allKeys.Where((key) => deviceContainer.Get(key) is ModBusRTU);
            Keys = new ObservableCollection<string>(selectKeys);
            SelectItem();
            notifyCollect = NotifyCollect.NotifyContainer["Error"];
        }
        public ObservableCollection<string> Keys { get; set; }

        private string key;
        public string Key
        {
            get { return key; }
            set
            {
                SetProperty(ref key, value);
                SendData(value);
            }
        }

        private void SelectItem()
        {
            if (Keys?.Count > 0)
            {
                Key = Keys[0];
            }
        }

        //需要弹窗 参数通过绑定传过来
        public ICommand AddCommand
        { get => new DelegateCommand(AddDevice); }

        private void AddDevice()
        {
            try
            {
                string param = string.Empty;
                _dialogService.ShowDialog("SubView", (IDialogResult result) =>
                {
                    ButtonResult result1 = result.Result;
                    param = result.Parameters.GetValue<string>("param1");
                });
                if (param != null && !Keys.Contains(param))
                { Keys.Add(param); }

                RTUParam rtuParam = new RTUParam();                  
                RTUParams.Add(param, rtuParam);

                ModBusRTU device = new ModBusRTU(param)
                {
                    Param = rtuParam
                };
                deviceContainer.Add(param, device);
                SelectItem();
            }
            catch (System.Exception ex)
            {
               notifyCollect.Add(ex.ToString());
            }
        }

        public ICommand DelCommand
        { get => new DelegateCommand(DelDevice); }

        private void DelDevice()
        {
            try
            {
                RTUParams.Remove(Key);
                //必须先删除容器里面的
                deviceContainer.Remove(Key);
                //再删除Keys里面的
                if (Keys.Contains(Key))
                { Keys.Remove(Key); }
                SelectItem();
            }
            catch (System.Exception ex)
            {
                notifyCollect.Add(ex.ToString());
            }         
        }

        private void SendData(string data)
        {
            if (!string.IsNullOrWhiteSpace(data))
            {
                //事件聚合器 选择rtu设备
                _ea.GetEvent<MessageEvent2>().Publish(data.ToString());
            }
        }
    }
}
